package com.zhu.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.commons.utils.R;
import com.zhu.service.edu.ThreadLocalService.TeacherThreadLocal;
import com.zhu.service.edu.feign.OssFeignService;
import com.zhu.service.edu.mapper.EduCourseMapper;
import com.zhu.service.edu.pojo.EduCourse;
import com.zhu.service.edu.pojo.EduTeacher;
import com.zhu.service.edu.mapper.EduTeacherMapper;
import com.zhu.service.edu.pojo.vo.TeacherQueryVo;
import com.zhu.service.edu.service.IEduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements IEduTeacherService {
    @Autowired
    private OssFeignService ossFeignService;
    /**
     * 分页查询教师列表
     * @param eduTeacherPage
     * @param teacherQueryVo
     * @return
     */
    @Override
    public IPage<EduTeacher> selectPage(Page<EduTeacher> eduTeacherPage, TeacherQueryVo teacherQueryVo) {
        //显示分页查询
        //1.按照sort字段排序
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        //2.根据条件查询
       if(teacherQueryVo==null){
           return baseMapper.selectPage(eduTeacherPage,queryWrapper);
       }
       //条件查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.likeRight("name",name);
        }
        if(level!=null){
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.ge("join_date",joinDateBegin);
        }
        if(StringUtils.isNotBlank(joinDateEnd)){
            queryWrapper.ge("join_date",joinDateEnd);
        }
        return baseMapper.selectPage(eduTeacherPage,queryWrapper);
    }

    /**
     * 根据关键字查询教师姓名
     * @param key
     * @return
     */
    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {
        LambdaQueryWrapper<EduTeacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduTeacher::getGmtModified);
        queryWrapper.likeRight(EduTeacher::getName,key);
        queryWrapper.last(" limit 5 ");
        queryWrapper.select(EduTeacher::getName);
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public boolean deleteAvatarByUrl(String id) {
        EduTeacher teacher = baseMapper.selectById(id);
        if(null==teacher){
            return false;
        }
        String avatar = teacher.getAvatar();
        String substringAfterLast = org.apache.commons.lang3.StringUtils.substringAfterLast(avatar, ".com/");
        System.out.println(substringAfterLast);
        if(StringUtils.isNotEmpty(substringAfterLast)){
            R r = ossFeignService.deleteFile(substringAfterLast);
            return r.getSuccess();
        }
        return false;
    }
    @Autowired
    private TeacherThreadLocal teacherThreadLocal;
    @Autowired
    private IEduTeacherService eduTeacherService;

    /**
     * 根据id逻辑删除教师
     * @param id
     * @return
     */
    @Override
    public boolean removeTeacherById(String id) {
        /**
         * 删除教师头像
         */
        //1.删除七牛云的图 在线程池执行
        teacherThreadLocal.deleteAvatarById(baseMapper,ossFeignService,eduTeacherService,id);
        //在数据库逻辑删除教师
        int i = baseMapper.deleteById(id);
        if(i==1){
            return  true;
        }
        return false;
    }
@Autowired
private EduTeacherMapper teacherMapper;
    @Autowired
    private EduCourseMapper courseMapper;
    @Override
    public Map<String, Object> selectTeacherInfo(String id) {
        //查询讲师
        EduTeacher eduTeacher = teacherMapper.selectById(id);
        //查询讲师所讲的课程
        LambdaQueryWrapper<EduCourse> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourse::getTeacherId,id);
        List<EduCourse> courseList = courseMapper.selectList(queryWrapper);
        Map<String, Object> map=new LinkedHashMap<>();
        map.put("teacher",eduTeacher);
        map.put("courseList",courseList);
        return map;
    }
}
